package com.mycompany.oj.Mapper.Impl;

import com.mycompany.oj.Mapper.LoginnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@Component
public class LoginMapperImpl implements LoginnMapper {

    @Autowired
    SqlOperator sqlOperator;

    //测试数据库连没连上
    public String test(String name) {
        String stmt = "select passwd from Account where username=?";
        List list = sqlOperator.query(stmt, name);
        if (list.isEmpty()) {
            return "UnKnown";
        } else {
            return (String) list.get(0);
        }
    }

    //登录
    @Override
    public List<Map<String, Object>> loginMapper(String username) {
        String stmt = "select passwd,state from Account where username=?";
        List<Map<String, Object>> list = sqlOperator.query(stmt, username);
        return list;

    }

    public List<Map<String, Object>> havenRegister(String username) {
        String stmt = "select id from Account where username=?";
        List<Map<String, Object>> list = sqlOperator.query(stmt, username);
        return list;

    }

    //注册
    @Override
    public String[] registerMapper(String username, String password, String state) {
        String stmt = "insert into Account(username,passwd,state) values(?,?,?)";
        String[] temp = sqlOperator.insert(stmt, username, password, state);
        return temp;
    }

    //注册个人信息
    @Override
    public String[] registerInformationMapper(String cellphone, String nickname, String realname, String sex, String school, String grade, String major, String schoolid, String path) {
        String stmt1 = "Select id from Account where username = ?";
        List<Map<String, Object>> temp1 = sqlOperator.query(stmt1, cellphone);
        String stmt2 = "INSERT into Student(schoolid,cellphone,Account_id,grade,major,school,student_name,nickname,plan_num,picture,sex,picture_old) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        String[] temp = sqlOperator.insert(stmt2, schoolid, cellphone, temp1.get(0).get("id"), grade, major, school, realname, nickname, 0, path, sex, 0);
        return temp;
    }

    //获取头像
    public List<Map<String, Object>> getPictureMapper(String username) {
        String stmt = "Select picture from Student,Account where Student.Account_id = Account.id and Account.username = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //设置头像
    public int setPictureMapper(String username, String path, String old_path) {
        String stmt = "Update Student set picture = ? ,picture_old = ? WHERE Student.Account_id = ?";
        int temp = sqlOperator.update(stmt, path, old_path, username);
        return temp;
    }

    public List<Map<String, Object>> getPictureMapperByusername(String username) {
        String stmt = "Select picture_old from Student WHERE Student.Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //忘记密码
    public int forgetMapper(String username, String password) {
        String stmt = "UPDATE Account set passwd = ? WHERE username = ?";
        int temp = sqlOperator.update(stmt, password, username);
        return temp;
    }

    @Override
    public String[] initFavorite(String username) {
        String stmt = "Insert into My_favorites(Student_id,favorites_name) Value(?,?)";
        String[] temp = sqlOperator.insert(stmt, username, "默认收藏夹");
        return temp;
    }

    public List<Map<String, Object>> getStatus(String username) {
        String stmt = "Select state from Account where username = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }


    public String[] getIp(String ip) {
        String stmt = "Insert into IP(ip,time) value(?,now())";
        String[] temp = sqlOperator.insert(stmt, ip);
        return temp;
    }
}
